{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Occurrences After Bigram"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: findOcurrences"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #Bigram 分词"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给出第一个词&nbsp;<code>first</code> 和第二个词&nbsp;<code>second</code>，考虑在某些文本&nbsp;<code>text</code>&nbsp;中可能以 <code>\"first second third\"</code> 形式出现的情况，其中&nbsp;<code>second</code>&nbsp;紧随&nbsp;<code>first</code>&nbsp;出现，<code>third</code>&nbsp;紧随&nbsp;<code>second</code>&nbsp;出现。</p>\n",
    "\n",
    "<p>对于每种这样的情况，将第三个词 \"<code>third</code>\" 添加到答案中，并返回答案。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>text = \"alice is a good girl she is a good student\", first = \"a\", second = \"good\"\n",
    "<strong>输出：</strong>[\"girl\",\"student\"]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>text = \"we will we will rock you\", first = \"we\", second = \"will\"\n",
    "<strong>输出：</strong>[\"we\",\"rock\"]\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= text.length &lt;= 1000</code></li>\n",
    "\t<li><code>text</code>&nbsp;由小写英文字母和空格组成</li>\n",
    "\t<li><code>text</code> 中的所有单词之间都由 <strong>单个空格字符</strong> 分隔</li>\n",
    "\t<li><code>1 &lt;= first.length, second.length &lt;= 10</code></li>\n",
    "\t<li><code>first</code> 和&nbsp;<code>second</code>&nbsp;由小写英文字母组成</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [occurrences-after-bigram](https://leetcode.cn/problems/occurrences-after-bigram/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [occurrences-after-bigram](https://leetcode.cn/problems/occurrences-after-bigram/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"alice is a good girl she is a good student\"\\n\"a\"\\n\"good\"', '\"we will we will rock you\"\\n\"we\"\\n\"will\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        text = text.split()\n",
    "        res = []\n",
    "        for i in range(2, len(text)):\n",
    "            if text[i-2] == first and text[i-1] == second: \n",
    "                res.append(text[i])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        text = text.split()\n",
    "        return [trd for fst, sed, trd in zip(text, text[1:], text[2:]) if fst == first and sed == second]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        res = []\n",
    "        text = text.split()\n",
    "        for i in range(len(text)-2):\n",
    "            if text[i]==first and text[i+1]==second:\n",
    "                res.append(text[i+2])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        res = text.split(\" \")\n",
    "        r = []\n",
    "        for i in range(len(res)-2):\n",
    "            if res[i] == first and res[i+1] == second:\n",
    "                r.append(res[i+2])\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        text = text.split(\" \")\n",
    "        res = []\n",
    "        for i in range(len(text)-2):\n",
    "            if text[i] == first and text[i+1] == second:\n",
    "                res.append(text[i+2])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        textword=text.split(' ')\n",
    "        n=len(textword)\n",
    "        res=[]\n",
    "        for i in range(n-2):\n",
    "            if textword[i]==first and textword[i+1]==second:\n",
    "                res.append(textword[i+2])\n",
    "            \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        text = text.split()\n",
    "        n = len(text)\n",
    "        res = []\n",
    "        for i in range(n - 2):\n",
    "            if text[i] == first and text[i + 1] == second:\n",
    "                res.append(text[i + 2])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        res = []\n",
    "        tmp = text.split(\" \")\n",
    "\n",
    "        for index, value in enumerate(tmp):\n",
    "            if index + 1 < len(tmp):\n",
    "                if value == first and tmp[index+1] == second:\n",
    "                    if index + 2 < len(tmp):\n",
    "                        res.append(tmp[index+2])\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        lst = text.split()\n",
    "        ans = []\n",
    "        for i in range(len(lst)-2):\n",
    "            if lst[i]==first and lst[i+1]==second:\n",
    "                ans.append(lst[i+2])\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        result = []\n",
    "        text_list = text.split(\" \")\n",
    "        for i in range(len(text_list)-2):\n",
    "            if text_list[i] == first and text_list[i+1] == second:\n",
    "                result.append(text_list[i+2])\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        a=text.split()\n",
    "        answer=[]\n",
    "        for i in range(len(a)-2):\n",
    "            if a[i]+a[i+1]==first+second:\n",
    "                answer.append(a[i+2])\n",
    "        return answer        \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        text = text.split(' ')\n",
    "        n = len(text)\n",
    "        i = 0\n",
    "        ans = []\n",
    "        while i<n-2:\n",
    "            if first in text[i:]:\n",
    "                inx = text.index(first,i)\n",
    "                if inx < n-2:\n",
    "                    if text[inx+1] == second:\n",
    "                        ans.append(text[inx+2])\n",
    "                    i = inx+1\n",
    "                else:\n",
    "                    break\n",
    "            else:\n",
    "                break\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        tt = text.split(' ')\n",
    "        res = []\n",
    "        for i in range(len(tt)-2):\n",
    "            if tt[i]==first and tt[i+1]==second:res.append(tt[i+2])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        test_l = text.split()\n",
    "        l = []\n",
    "        for i in range(len(test_l)):\n",
    "            if test_l[i] == first:\n",
    "                if (i + 2) < len(test_l) and test_l[i + 1] == second:\n",
    "                    l.append(test_l[i+2])\n",
    "        return l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        text = text.split()\n",
    "        n = len(text)\n",
    "        res = []\n",
    "        for i in range(n - 2):\n",
    "            if text[i] == first and text[i + 1] == second:\n",
    "                res.append(text[i + 2])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        text = text.split(' ')\n",
    "        length = len(text)\n",
    "        ans = []\n",
    "        if length <= 2:\n",
    "            return ans\n",
    "        for idx in range(length - 2):\n",
    "            if text[idx] == first and text[idx+1] == second:\n",
    "                ans.append(text[idx+2])\n",
    "        return ans\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        b=text.split(' ')\n",
    "        c=[]\n",
    "        for i in range(len(b)):\n",
    "            if i<len(b)-2 and b[i]==first and b[i+1]==second:\n",
    "                c.append(b[i+2])\n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        l=[]\n",
    "        a=text.split()\n",
    "        for i in range(len(a)-2):\n",
    "         if a[i]==first and a[i+1]==second:\n",
    "                l.append(a[i+2])\n",
    "        return l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        words = text.split()\n",
    "        return [words[i] for i in range(2, len(words)) if words[i - 2] == first and words[i - 1] == second]\n",
    "\n",
    "# 作者：力扣官方题解\n",
    "# 链接：https://leetcode.cn/problems/occurrences-after-bigram/solutions/1174360/bigram-fen-ci-by-leetcode-solution-7q3e/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        s = text.split()\n",
    "        l = len(s)\n",
    "        ans = list()\n",
    "        for i in range(l - 2):\n",
    "            if s[i] == first and s[i + 1] == second:\n",
    "                ans.append(s[i + 2])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        text_words = text.split(\" \")\n",
    "        n = len(text_words)\n",
    "        ans = []\n",
    "        for i in range(n) :\n",
    "            if text_words[i] == first  :\n",
    "                if i + 1 < n and text_words[i+1] == second :\n",
    "                    if i + 2 < n :\n",
    "                        ans.append(text_words[i+2])\n",
    "        return ans  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        return re.findall(fr\"(?<=\\b{first} {second} )(\\w+)\", text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        text_list = text.split(' ')\n",
    "        print(text_list)\n",
    "        ans = []\n",
    "        for idx in range(len(text_list)):\n",
    "            if idx>1 and text_list[idx-1]==second and text_list[idx-2]==first:\n",
    "                ans.append(text_list[idx])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        text_list = text.split(\" \")\n",
    "        lis = []\n",
    "        for i in range(1,len(text_list)-1):\n",
    "            if text_list[i-1] == first and text_list[i] == second:\n",
    "                lis.append(text_list[i+1])\n",
    "        return lis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "\n",
    "        t=text.split(' ')\n",
    "        x=[index for index,value in enumerate(t) if value==second and index!=0 and index!=len(t)-1]\n",
    "        return [t[i+1] for i in x if t[i-1]==first]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        text=text.split()\n",
    "        strs=[]\n",
    "        long=len(text)\n",
    "        for t,i in enumerate(text):\n",
    "            if i==first and t<long-2:\n",
    "                if text[t+1]==second:\n",
    "                    strs.append(text[t+2])\n",
    "        return strs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text1: str, first: str, second: str) -> List[str]:\n",
    "        text = text1.split()\n",
    "        res = []\n",
    "        n = len(text)\n",
    "        for i in range(n - 2):\n",
    "            if text[i] == first and text[i+1] == second:\n",
    "                res.append(text[i+2])\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        text=text.split()\n",
    "        strs=[]\n",
    "        long=len(text)-2\n",
    "        for t,i in enumerate(text):\n",
    "            if i==first and t<long:\n",
    "                if text[t+1]==second:\n",
    "                    strs.append(text[t+2])\n",
    "        return strs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        a=text.split()\n",
    "        b=[]\n",
    "        for i in range(2,len(a)):\n",
    "            if a[i-2]==first and a[i-1]==second:\n",
    "                b.append(a[i])\n",
    "        return b\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        tex=text.split(\" \")\n",
    "        ans=[]\n",
    "        for i in range(len(tex)-2):\n",
    "            if tex[i] == first and tex[i+1]==second:\n",
    "                ans.append(tex[i+2])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        text = text.split(\" \")\n",
    "        print(text)\n",
    "        ans = []\n",
    "        firstFlag = 0\n",
    "        # SecondFlag = 0\n",
    "        for i in range(len(text) - 2):\n",
    "            if (text[i] == first and text[i+1] == second):\n",
    "                ans.append(text[i+2])\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        return re.findall(fr\"(?<=\\b{first} {second} )(\\w+)\", text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        words = text.split(' ')\n",
    "        return [words[i + 2] for i in range(len(words) - 2) if words[i] == first and words[i + 1] == second]\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        text=text.split()\n",
    "        strs=[]\n",
    "        long=len(text)-2\n",
    "        for t,i in enumerate(text):\n",
    "            if i==first and t<long:\n",
    "                if text[t+1]==second:\n",
    "                    strs.append(text[t+2])\n",
    "        return strs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        list1 = []\n",
    "        for i ,w in enumerate(words:=text.split(\" \")):\n",
    "            if i<len(words)-2 and w==first and words[i+1]==second:\n",
    "                list1.append(words[i+2])\n",
    "        return list1        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        words = text.split(' ')\n",
    "        res = []\n",
    "        for i, word in enumerate(words):\n",
    "            if i + 2 < len(words) and word == first and words[i + 1] == second:\n",
    "                res.append(words[i+2])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\r\n",
    "        text = text.split(' ')\r\n",
    "        ans = list()\r\n",
    "        a = 0\r\n",
    "        while a < len(text)-2:\r\n",
    "            if text[a] == first and text[a+1] == second:\r\n",
    "                ans.append(text[a+2])\r\n",
    "                a += 1\r\n",
    "            else:\r\n",
    "                a += 1\r\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        text = text.split()\n",
    "        fhsz = []\n",
    "        for i in range(1, len(text)-1):\n",
    "            if text[i] == second and text[i-1] == first:\n",
    "                fhsz.append(text[i+1])\n",
    "        return fhsz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        text=text.split(' ')\n",
    "        i=0\n",
    "        result=[]\n",
    "        while i<len(text)-2:\n",
    "            if first==text[i] and second==text[i+1]:\n",
    "                result.append(text[i+2])\n",
    "            i+=1\n",
    "        return result \n",
    "                 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        words = text.split() \n",
    "        return [words[i] for i in range(2,len(words)) if words[i-2] == first and words[i-1]== second]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        list_str = text.split()\n",
    "        ans = []\n",
    "        for i in range(len(list_str) - 2):\n",
    "            if first == list_str[i] and second == list_str[i + 1]:\n",
    "                ans.append(list_str[i + 2])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        ans = []\n",
    "        ls = text.split(\" \")\n",
    "        for i, w in enumerate(ls[:-2]):\n",
    "            if w == first and ls[i+1] == second:\n",
    "                ans.append(ls[i+2])\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        r=[]\n",
    "        lt=text.split()\n",
    "        for i in range(len(lt)-2):\n",
    "            if lt[i]==first and lt[i+1]==second:\n",
    "                r.append(lt[i+2])\n",
    "        return r\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        t=list(map(str,text.split()))\n",
    "        ans=[]\n",
    "        for i in range(len(t)):\n",
    "            if(i<len(t)-2 and t[i]==first and t[i+1]==second):\n",
    "                ans.append(t[i+2])\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        lst=list(text.split(\" \"))\n",
    "        ans=list()\n",
    "        for i in range(len(lst)-2):\n",
    "            if lst[i]==first and lst[i+1]==second:\n",
    "                ans.append(lst[i+2])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        res = []\n",
    "\n",
    "        for i, w in enumerate(words:=text.split(\" \")):\n",
    "            if i < len(words) - 2 and w == first and words[i+1] == second:\n",
    "                res.append(words[i+2])\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        textword=text.split(' ')\n",
    "        n=len(textword)\n",
    "        i=0\n",
    "        res=[]\n",
    "        while i<n-2:\n",
    "            if textword[i]==first and textword[i+1]==second:\n",
    "                res.append(textword[i+2])\n",
    "            i+=1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        text = text.split()\n",
    "        ans = []\n",
    "        for i in range(2, len(text)):\n",
    "            if text[i-2] == first and text[i-1] == second:\n",
    "                ans.append(text[i])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "\n",
    "        # Split the original text\n",
    "        words = text.split()\n",
    "\n",
    "        return [\n",
    "            words[i + 2] for i in range(len(words) - 2)\n",
    "            if words[i] == first and words[i + 1] == second\n",
    "        ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        ts=text.split()\n",
    "        # print(ts)\n",
    "        n=len(ts)\n",
    "        res=[]\n",
    "        for i in range(2,n):\n",
    "            if ts[i-1]==second and ts[i-2]==first:\n",
    "                res.append(ts[i])\n",
    "        print(res)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        l = text.split()\n",
    "        res = []\n",
    "        for i in range(len(l) - 2):\n",
    "            if l[i] == first and l[i + 1] == second:\n",
    "                res.append(l[i + 2])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        ans = text.split(\" \")\n",
    "        res =[]\n",
    "        for i in range(len(ans)-2):\n",
    "            if ans[i]==first and ans[i+1]==second:\n",
    "                res.append(ans[i+2])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        third = []\n",
    "        word = text.split()\n",
    "        \n",
    "        for i in range(len(word)-2):\n",
    "            if word[i] == first and word[i+1] == second:\n",
    "                third.append(word[i+2])\n",
    "        \n",
    "        return third"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        lst = text.split(\" \")\n",
    "        ans = []\n",
    "        for i in range(len(lst) - 2):\n",
    "            if lst[i] == first and lst[i + 1] == second:\n",
    "                ans.append(lst[i + 2])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        text = text.split(' ')\n",
    "        return [text[i] for i in range(2,len(text)) if text[i-2] == first and text[i-1] == second]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        result = []\n",
    "        text = text.split(\" \")\n",
    "        if len(text) < 3:\n",
    "            return result\n",
    "        for i in range(2,len(text)):\n",
    "            if text[i-1] == second and text[i-2] == first:\n",
    "                result.append(text[i])\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        h = text.split(' ')\n",
    "        res = []\n",
    "        for i in range(2, len(h)):\n",
    "            if h[i - 1] == second and h[i - 2] == first:\n",
    "                res.append(h[i])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        text = text.split(\" \")\n",
    "        res = []\n",
    "        for i in range(len(text)-2):\n",
    "            if text[i] == first and text[i+1] == second:\n",
    "                res.append(text[i+2])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        res = []\n",
    "        tmp = text.split(\" \")\n",
    "\n",
    "        for index, value in enumerate(tmp):\n",
    "            if index + 1 < len(tmp):\n",
    "                if value == first and tmp[index+1] == second:\n",
    "                    if index + 2 < len(tmp):\n",
    "                        res.append(tmp[index+2])\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        l = text.split(\" \")\n",
    "        ans = []\n",
    "        for i in range(len(l)):\n",
    "            if l[i] == second:\n",
    "                if 0 < i < len(l)-1 and l[i-1] == first:\n",
    "                    ans.append(l[i+1])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        t = text.split()\n",
    "        return [c for a, b, c in zip(t, t[1:], t[2:]) if a == first and b == second]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        ans, words = [], text.split()\n",
    "        for i in range(len(words) - 2):\n",
    "            if words[i] == first and words[i + 1] == second:\n",
    "                ans.append(words[i + 2])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        ans = []\n",
    "        words = text.split(' ')\n",
    "        n = len(words)\n",
    "        start, end = 0,2\n",
    "        while end < n:\n",
    "            if words[start] == first and words[start+1] == second:\n",
    "                ans.append(words[end])\n",
    "            start += 1\n",
    "            end += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        words = text.split()\n",
    "        return [words[i] for i in range(2, len(words)) if words[i - 2] == first and words[i - 1] == second]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        ls = text.split()\n",
    "        fs=[]\n",
    "        for i in range(len(ls)-2):\n",
    "            if ls[i]==first and ls[i+1]==second:\n",
    "                fs.append(ls[i+2])\n",
    "        return fs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        words = text.split(' ')\n",
    "\n",
    "        n = len(words)\n",
    "\n",
    "        ans = []\n",
    "        for i in range(n-1):\n",
    "            if words[i] == first and  i + 1 < n and  words[i+1] == second and i + 2 < n:\n",
    "                ans.append(words[i+2])\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        textword=text.split(' ')\n",
    "        n=len(textword)\n",
    "        i=0\n",
    "        res=[]\n",
    "        for i in range(n-2):\n",
    "            if textword[i]==first and textword[i+1]==second:\n",
    "                res.append(textword[i+2])\n",
    "            \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "\n",
    "\n",
    "\n",
    "        word = text.split(' ')\n",
    "        res = []\n",
    "\n",
    "        for i in range(2, len(word)):\n",
    "\n",
    "            if word[i - 2] == first and word[i - 1] == second:\n",
    "\n",
    "                res.append(word[i])\n",
    "\n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        res = []\n",
    "        sentence = text.split()\n",
    "        for index in range(1, len(sentence)):\n",
    "            if sentence[index-1] == first and sentence[index] == second and index+1 < len(sentence):\n",
    "                res.append(sentence[index+1])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n",
    "        text = text.split(' ')\n",
    "        return [text[i] for i in range(2,len(text)) if text[i-2] == first and text[i-1] == second]\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
